{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "430e98a9-a159-4cf1-93a1-452e9a48681a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import (\n",
    "    SystemMessagePromptTemplate,\n",
    "    AIMessagePromptTemplate,\n",
    "    HumanMessagePromptTemplate,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9b64d8f7-48bf-409b-a6f0-e0dfc397cb16",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_template_text=\"你是一位专业的翻译，能够将{input_language}翻译成{output_language}，并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本，不要有任何其它内容。\"\n",
    "system_prompt_template = SystemMessagePromptTemplate.from_template(system_template_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "37109f89-6f85-4cda-ab98-f8c9d0b5b691",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input_language', 'output_language'], template='你是一位专业的翻译，能够将{input_language}翻译成{output_language}，并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本，不要有任何其它内容。'))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "system_prompt_template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1cfa77bc-4cb7-485f-bbb8-6c9134a687fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['input_language', 'output_language']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "system_prompt_template.input_variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d750991b-3d95-425f-85e0-6496e7de6b94",
   "metadata": {},
   "outputs": [],
   "source": [
    "human_template_text=\"文本：{text}\\n语言风格：{style}\"\n",
    "human_prompt_template = HumanMessagePromptTemplate.from_template(human_template_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "52fc12d4-68f4-4207-a0e1-d388e9efd392",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['style', 'text']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "human_prompt_template.input_variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a33fa030-85dc-480b-8ecd-e95f376d9a18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SystemMessage(content='你是一位专业的翻译，能够将英语翻译成汉语，并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本，不要有任何其它内容。')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "system_prompt = system_prompt_template.format(input_language=\"英语\", output_language=\"汉语\")\n",
    "system_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "22b35876-26b8-436b-a07f-b417169dee1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HumanMessage(content=\"文本：I'm so hungry I could eat a horse\\n语言风格：文言文\")"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "human_prompt = human_prompt_template.format(text=\"I'm so hungry I could eat a horse\", style=\"文言文\")\n",
    "human_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3d805402-0f0a-4b3c-a51f-ab7edb66ac86",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bc6aba46-6917-43d2-b48d-3064c401c1fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
    "response = model.invoke([\n",
    "    system_prompt,\n",
    "    human_prompt\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "24936269-fb7e-4757-a6ea-e6d7f3384b6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "吾飢甚，能食千里馬。\n"
     ]
    }
   ],
   "source": [
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "075c3017-6450-4f67-826a-f08fa0160b0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_variables = [\n",
    "    {\n",
    "        \"input_language\": \"英语\",\n",
    "        \"output_language\": \"汉语\",\n",
    "        \"text\": \"I'm so hungry I could eat a horse\",\n",
    "        \"style\": \"文言文\"\n",
    "    },\n",
    "    {\n",
    "        \"input_language\": \"法语\",\n",
    "        \"output_language\": \"英语\",\n",
    "        \"text\": \"Je suis désolé pour ce que tu as fait\",\n",
    "        \"style\": \"古英语\"\n",
    "    },\n",
    "    {\n",
    "        \"input_language\": \"俄语\",\n",
    "        \"output_language\": \"意大利语\",\n",
    "        \"text\": \"Сегодня отличная погода\",\n",
    "        \"style\": \"网络用语\"\n",
    "    },\n",
    "    {\n",
    "        \"input_language\": \"韩语\",\n",
    "        \"output_language\": \"日语\",\n",
    "        \"text\": \"너 정말 짜증나\",\n",
    "        \"style\": \"口语\"\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "44d397ea-1457-4ca1-b65e-7475cad01efc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "饥肠辘辘，饥不择食。\n",
      "I am sorry for what thou hast done.\n",
      "Oggi il tempo è fantastico\n",
      "お前、本当にイライラするな。\n"
     ]
    }
   ],
   "source": [
    "for input in input_variables:\n",
    "    response = model.invoke([\n",
    "        system_prompt_template.format(input_language=input[\"input_language\"], output_language=input[\"output_language\"]), \n",
    "        human_prompt_template.format(text=input[\"text\"], style=input[\"style\"])])\n",
    "    print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f986a048-e0cf-451f-bce7-58f98292ca98",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1d28bb33-a121-476c-ae5d-cb4b4513eb9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"你是一位专业的翻译，能够将{input_language}翻译成{output_language}，并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本，不要有任何其它内容。\"),\n",
    "        (\"human\", \"文本：{text}\\n语言风格：{style}\"),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ba73a09d-d1bb-4882-9bd5-505ce3ce1962",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['input_language', 'output_language', 'style', 'text']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_template.input_variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a425d7f2-b16b-4b3a-94a8-d5d7d6b4a058",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='你是一位专业的翻译，能够将英语翻译成汉语，并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本，不要有任何其它内容。'), HumanMessage(content=\"文本：I'm so hungry I could eat a horse\\n语言风格：文言文\")])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_value = prompt_template.invoke({\"input_language\": \"英语\", \"output_language\": \"汉语\", \n",
    "                                       \"text\":\"I'm so hungry I could eat a horse\", \"style\": \"文言文\"})\n",
    "prompt_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0d6f7314-9c01-4569-8ef6-62bf49dee402",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content='你是一位专业的翻译，能够将英语翻译成汉语，并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本，不要有任何其它内容。'),\n",
       " HumanMessage(content=\"文本：I'm so hungry I could eat a horse\\n语言风格：文言文\")]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_value.messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1e281925-928e-4430-99e2-6aebe0669441",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
    "response = model.invoke(prompt_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8e4f1857-2064-4d27-9925-377350e3da7c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='吾飢甚，可食馬焉。')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3fab8667-12cc-4000-b144-6b7e7cf5deba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'吾飢甚，可食馬焉。'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e228f806-dd75-4ed9-b46a-716854019515",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_variables = [\n",
    "    {\n",
    "        \"input_language\": \"英语\",\n",
    "        \"output_language\": \"汉语\",\n",
    "        \"text\": \"I'm so hungry I could eat a horse\",\n",
    "        \"style\": \"文言文\"\n",
    "    },\n",
    "    {\n",
    "        \"input_language\": \"法语\",\n",
    "        \"output_language\": \"英语\",\n",
    "        \"text\": \"Je suis désolé pour ce que tu as fait\",\n",
    "        \"style\": \"古英语\"\n",
    "    },\n",
    "    {\n",
    "        \"input_language\": \"俄语\",\n",
    "        \"output_language\": \"意大利语\",\n",
    "        \"text\": \"Сегодня отличная погода\",\n",
    "        \"style\": \"网络用语\"\n",
    "    },\n",
    "    {\n",
    "        \"input_language\": \"韩语\",\n",
    "        \"output_language\": \"日语\",\n",
    "        \"text\": \"너 정말 짜증나\",\n",
    "        \"style\": \"口语\"\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "85f1f18d-4047-43b8-905a-05bd4ddcdc80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "饥饿难耐，食欲如虎，且可食马也。\n",
      "I am sorry for what thou hast done.\n",
      "Oggi c'è un tempo fantastico\n",
      "お前、本当にイラつくな。\n"
     ]
    }
   ],
   "source": [
    "for input in input_variables:\n",
    "    response = model.invoke(prompt_template.invoke({\"input_language\": input[\"input_language\"], \"output_language\": input[\"output_language\"], \n",
    "                                                    \"text\":input[\"text\"], \"style\": input[\"style\"]}))\n",
    "    print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "770b0882-3ce3-499f-8e3f-021411ec71c0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
